package com.yao.sdk.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class ExtLinearLayoutManager extends LinearLayoutManager {
    // RecyclerView高度随Item自适应
    public ExtLinearLayoutManager(Context context) {
        super(context);
    }
    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, final int widthSpec,final int heightSpec) {
        int itemCount = state.getItemCount();
        if (itemCount > 0) {
            View view = recycler.getViewForPosition(0);
            if(view != null){
                measureChild(view, widthSpec, heightSpec);
                int measuredWidth = View.MeasureSpec.getSize(widthSpec);
                int measuredHeight = view.getMeasuredHeight();
                setMeasuredDimension(measuredWidth, measuredHeight*itemCount);
            }
        } else {
            super.onMeasure(recycler, state, widthSpec, heightSpec);
        }

    }
}